package com.wlient.basic.pachong; /**
 * @author Wlient
 * @date 2020/9/14 23:29
 */

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SpiderDemo {
    public static void main(String[] args) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5, 20, 5L, TimeUnit.SECONDS, new ArrayBlockingQueue<>(20)
        );
        // 线程池对拒绝任务(无线程可用)的处理策略
        threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());

        List<String> urls = new ArrayList<>();
        for (int i = 0; i <= 400; i += 2) {
            String url = "https://movie.douban.com/j/search_subjects?type=movie&tag=%E7%83%AD%E9%97%A8&sort=time&page_limit=2&page_start=" + i;
            urls.add(url);
        }
        long begin = System.currentTimeMillis();
        urls.stream().forEach(m -> {
            CompletableFuture.runAsync(() -> {
                try {
                    MoreMovice movice = new MoreMovice();
                    movice.getMovieDetail(m);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }, threadPoolExecutor);
        });


        System.out.println(System.currentTimeMillis() - begin);

    }
}
